package message.controller;

import message.mapper.FriendmapMapper;
import message.model.Friendmap;
import message.model.FriendmapExample;
import message.model.User;
import message.service.FriendService;
import message.service.UserService;
import message.util.DigestUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;

	private FriendService friendService;

	private FriendmapMapper friendmap;
	private final Log logger = LogFactory.getLog(getClass());
	
	@RequestMapping("/login")
	public String tologin(){
		return "login";
	}
	/**
	 * @author:�����
	 * @date: 2016��6��21��
	 * @return ModelAndView 
	 * @describe��ʹ��MD5�㷨��������м��ܣ���½��֤
	 */
	@RequestMapping("/loginvalid")
	public ModelAndView loginvalid(User user, HttpSession session) throws NoSuchAlgorithmException{
		User host=userService.findUserByQq(user.getQq());  //ͨ��qq��ѯ�Ƿ���ڴ��û�
		ModelAndView mav=new ModelAndView();  //ʵ����һ��ModelAndView����
		mav.setViewName("login");    // ��ʼ���߼���ͼΪlogin.jsp
		if(host!=null){
			//��ǰ�˴�����ֵͨ��MD5���ܽ��жԱ�
			boolean suc=host.getPassword().equals(DigestUtil.getMD5(user.getPassword().getBytes()));
			if(suc){
				mav.addObject("login_suc", "��½�ɹ�");
				session.setAttribute("host", host);  //���û���Ϣ����session
				 java.util.Date  date=new java.util.Date();
				 java.sql.Timestamp time=new java.sql.Timestamp(date.getTime()); //���뵱ǰʱ��
				 User user1=new User();
				 user1.setLastvisit(time);   
				 userService.updateUser(user1);  //�����û������һ�ε�¼ʱ��
				 logger.info("QQ����Ϊ��"+host.getQq()+"���û���½ϵͳ");
				mav.setViewName("redirect:/user/feelallperson");   //����½�ɹ���ת����ҳ��
			}
			else{
				mav.addObject("login_fail", "�������");
			}
		}
		else{
			mav.addObject("login_fail", "�˺Ų�����");
		}
		return mav;
	}
   
  /**
  	 * @author: �����
  	 * @date: 2016��6��22��
  	 * @param userid
  	 * @return  ��������ѯ���û���Ϣ
  	 * @describe��ͨ��ǰ�˴�����userid,��ѯ����ѵ��û���Ϣ
   */
	@RequestMapping("/showinfor")
	public ModelAndView showinfo(@RequestParam(value="friendid")int userid,HttpSession session){
		ModelAndView mav=new ModelAndView();
		User user=userService.findUserById(userid);  //��ȡ���ѵ��û���Ϣ
		String birthdayString=1900+user.getBirthday().getYear()+"��"+(user.getBirthday().getMonth()+1)+"��"+user.getBirthday().getDate()+"��    ";
		user.setBirthdayString(birthdayString);  //��ʱ���ʽ�ı���й���ɫ��ʱ���ʽ
		User host=(User)session.getAttribute("host");  //��ȡ�Լ����û���Ϣ�����ڲ����Լ��ͺ��ѵ���ϵ
		FriendmapExample example=new FriendmapExample();
		FriendmapExample.Criteria criteria=example.createCriteria();
		criteria.andUseridEqualTo(userid);
		criteria.andFriendidEqualTo(host.getUserid()); //���Լ�Ϊ��������ʱ�ĺ��Ѷ�
		
		FriendmapExample.Criteria criteria2=example.createCriteria();
		criteria2.andFriendidEqualTo(userid);  
		criteria2.andUseridEqualTo(host.getUserid());  //���Լ�Ϊ������ʱ�ĺ��Ѷ�
		example.or(criteria2);    //��or��ѯ
		 //or��䡣�൱��select  state from friendmap WHERE ( userid = '5'  ) or( friendid = '5'  )   
		int state=friendmap.selectStateByExample(example);
		Friendmap friendm=(Friendmap)friendmap.selectByExample(example).get(0);
		String addfriendtimeString=1900+friendm.getAddtime().getYear()+"��"+(friendm.getAddtime().getMonth()+1)+"��"+friendm.getAddtime().getDate()+"��       "
				+friendm.getAddtime().getHours()+": "+friendm.getAddtime().getMinutes()+": "+friendm.getAddtime().getSeconds();
		mav.addObject("state", state);  //����״̬��Ϣ��������ǰ���ж϶Բ�ͬ�ĺ���ӵ�еĲ�ͬ��Ȩ��
		mav.addObject("user", user);
		mav.addObject("addfriendtimeString",addfriendtimeString);
		mav.setViewName("friendinfo");
		//��־����
		logger.info("QQ����Ϊ��"+host.getQq()+"���û��鿴�˺���"+user.getQq()+"�Ļ�����Ϣ");
		return mav;
	}
	/* 
 	 * @author: �����
 	 * @date: 2016��6��22��
 	 * @param userid
 	 * @return  ��������ѯ���Լ����û���Ϣ
 	 * @describe��ͨ��ǰ�˴�����userid,��ѯ�Լ��Ļ�����Ϣ
  */
	@RequestMapping("/showhostinfor")
	public ModelAndView showinfo(HttpSession session){
		User host=(User)session.getAttribute("host");
		ModelAndView mav=new ModelAndView();
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		  String birthdayString = formatter.format(host.getBirthday());
		host.setBirthdayString(birthdayString);  //��ʱ���ʽ�ı���й���ɫ��ʱ���ʽ
		String lastvisitString=1900+host.getLastvisit().getYear()+"��"+(host.getLastvisit().getMonth()+1)+"��"+host.getLastvisit().getDate()+"��       "
				+host.getLastvisit().getHours()+": "+host.getLastvisit().getMinutes()+": "+host.getLastvisit().getSeconds();
		
		host.setLastvisitString(lastvisitString);  //��ʱ���ʽ�ı���й���ɫ��ʱ���ʽ
		mav.addObject("host", host);
		mav.setViewName("index");
		logger.info("QQ����Ϊ��"+host.getQq()+"���û��鿴���Լ��Ļ�����Ϣ");
		return mav;
	}
	
	/**
		 * @author: �����
		 * @date: 2016��6��26��
		 * @param selectcontent ����ѯ����
		 * @param selectmethod ����ѯ��ʽ
		 * @param currentPage
		 * @describe�� ���Һ��ѣ�ͨ���ǳƺ�qq�����ѯ��
	 */
	@RequestMapping("/findFriend")
	public ModelAndView findFriend(String selectcontent,String selectmethod,Integer currentPage,HttpSession session ){
		ModelAndView mav=new ModelAndView();
		User host=(User)session.getAttribute("host");
		if(selectcontent!=null&&selectmethod!=null){   //��ѯ���ݺͷ�������Ϊ�յ������
			logger.info("QQ����Ϊ��"+host.getQq()+"���û�ͨ���û��������ˣ�"+selectmethod+"���û�");
			if(currentPage==null) currentPage=1;  //�紫����ֵ�ǿգ�����ʾ��һҳ
//			PageInfo result;
//			if(selectmethod.equals("username")){
//				 result=userService.findUserByQqOrUsername(selectcontent, null, currentPage);  //���պ��ѽ����ҳ��Ϣ
//			}else{
//				logger.info("QQ����Ϊ��"+host.getQq()+"���û��鿴���Լ��Ļ�����Ϣ");
//				 result=userService.findUserByQqOrUsername(null,selectcontent, currentPage);//���պ��ѽ����ҳ��Ϣ
//			}
//			mav.addObject("friendresult", result);
//			if(result.getList().size()<=0){
//				mav.addObject("resultisnull", "û�в�ѯ�����û���");
//				logger.info("QQ����Ϊ��"+host.getQq()+"���û�ͨ���û����������ǳ�Ϊ��"+selectmethod+"�ĺ��ѡ������δ���ҵ�");
//			}
		}
		mav.setViewName("askforfriend");
		return mav;
	}

	
	@RequestMapping("/logOut")
	public String logOut(HttpSession session){
		User host=(User)session.getAttribute("host");
		logger.info("QQ����Ϊ��"+host.getQq()+"���û�ͨ���˳���ϵͳ");
		session.invalidate();  //������session��������ϢĨȥ
		return "redirect:/user/login";
	}
	@RequestMapping("/tomodifyInfor")
	public String tomidifyInfor(){
		return "modifyInfor";
	}
	
}
